"use client"

import { getHistoryData } from "@/api/team/tember/history"
import { HistoryType } from "@/type/history";
import { Button, getKeyValue, Spinner, Table, TableBody, TableCell, TableColumn, TableHeader, TableRow } from "@nextui-org/react";
import { useEffect, useState } from "react"

const History = ({ team_id }: { team_id: number }) => {

    const [list, setList] = useState<HistoryType[]>([])

    const getHistory = async () => {
        const reuslt = await getHistoryData(team_id ?? 0)
        if (reuslt.code == 200) {
            setList(reuslt.data)
        }
        setIsLoading(false)
    }

    useEffect(() => {
        getHistory()
    }, [])



    const [isLoading, setIsLoading] = useState<boolean>(true);


    return (
        <>
            <Table
                isHeaderSticky
                classNames={{
                    base: "max-h-[88vh] overflow-scroll",
                }}
            >
                <TableHeader>
                    <TableColumn align="center" key="id">Id</TableColumn>
                    <TableColumn align="center" key="filename">FileName</TableColumn>
                    <TableColumn align="center" key="actions">Actions</TableColumn>
                </TableHeader>
                <TableBody
                    isLoading={isLoading}
                    items={list}
                    loadingContent={<Spinner color="white" />}
                    emptyContent={
                        <p>没有任何历史记录</p>
                    }
                >
                    {(item) => (
                        <TableRow key={item.id}>
                            {(columnKey) =>
                                <TableCell>
                                    {columnKey == "actions" ? <Button onPress={() => {
                                        return item.url
                                    }} isIconOnly as="a" href={"http://localhost:8080" + item.url} color={undefined} variant="shadow" size="sm" className="text-base">🔗</Button> : getKeyValue(item, columnKey)}
                                </TableCell>
                            }
                        </TableRow>
                    )}
                </TableBody>
            </Table>
        </>
    )
}

export default History